Validation - RegularExpressionValidator

Writing about RegularExpressionValidator is a bit more difficult, because it is essential to actually use it for the knowledge of expressions, however, Regular Expression Validator is one of the most useful valators because it uses the validity of any type of string Can be done to verify. If you do not feel like learning to be regular, but still feel like this validator, take a look around the Internet. You can get many pre-built expressions, which can be very useful to you here is an example where we need 4 digit numbers - nothing else is acceptable:


4 digit number:<br />
<asp:TextBox runat="server" id="txtNumber" />
<asp:RegularExpressionValidator runat="server" id="rexNumber" controltovalidate="txtNumber" validationexpression="^[0-9]{4}$" errormessage="Please enter a 4 digit number!" />

The only new feature that we use is statutory expression. Only regular expressions are kept in this feature because regular expressions are beyond the scope of this tutorial, I will not try to explain it, apart from this, it justifies the validator that the value of 4 digits is acceptable, and nothing else. You can use RegularExpressionValidator for almost everything, for example validating e-mail or url

Try switching to design and select RegularExpressionValidator on your page. Now there is a look in the properties tab of Visual Studio. The Validation Expression feature has a button, and if you click it, you will have access to some default expressions distributed by the ASP.NET team, for various purposes. Just select one of them